﻿//namespace Admin.NET.Application.Job;

///// <summary>
///// 物料在库时间管控任务（提前1/4预警时间）
///// </summary>
//public class MaterialInTheWareHouseTimeReminder : IJob
//{
//    #region 关系注入
//    private readonly SqlSugarRepository<EG_WMS_Inventory> _Inventory = App.GetService<SqlSugarRepository<EG_WMS_Inventory>>();
//    private readonly SqlSugarRepository<EG_WMS_Materiel> _Materiel = App.GetService<SqlSugarRepository<EG_WMS_Materiel>>();
//    private readonly SqlSugarRepository<EG_WMS_WorkBin> _WorkBin = App.GetService<SqlSugarRepository<EG_WMS_WorkBin>>();
//    private readonly SqlSugarRepository<EG_WMS_MaterielLevel> _MaterielLevel = App.GetService<SqlSugarRepository<EG_WMS_MaterielLevel>>();
//    #endregion

//    public MaterialInTheWareHouseTimeReminder()
//    {

//    }

//    public async Task<List<MaterielStorageTimeWarringDto>> ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
//    {
//        List<MaterielStorageTimeWarringDto> datas = new List<MaterielStorageTimeWarringDto>();
//        // 查询在库数据
//        var _invdata = _Inventory.AsQueryable()
//                                 .InnerJoin<EG_WMS_InventoryDetail>((a, b) => a.InventoryNum == b.InventoryNum)
//                                 .InnerJoin<EG_WMS_Materiel>((a, b, c) => a.MaterielNum == c.MaterielNum)
//                                 .Where((a, b, c) => a.OutboundStatus == 0 && c.InventoryDateTime != null)
//                                 .GroupBy((a, b, c) => a.MaterielNum)
//                                 .Select((a, b, c) => new
//                                 {
//                                     a.MaterielNum,
//                                     a.ICountAll,
//                                     a.ProductionDate,
//                                     b.StorageNum,
//                                     b.WorkBinNum,
//                                     c.MaterielName,
//                                 })
//                                 .ToList();

//        DateTime oldtime = new DateTime();
//        DateTime newtime = new DateTime();
//        DateTime newtimequarter = new DateTime();
//        DateTime nowtime = DateTime.Now;
//        for (int i = 0; i < _invdata.Count; i++)
//        {
//            // 查询当前物料的预警时间
//            EG_WMS_Materiel _materieldata = await _Materiel.GetFirstAsync(x => x.MaterielNum == _invdata[i].MaterielNum);
//            // 生产时间
//            oldtime = (DateTime)_invdata[i].ProductionDate;
//            // 生产时间加上提醒时间
//            newtime = oldtime.AddHours((double)_materieldata.InventoryDateTime);
//            // 生产时间加上提醒时间减去提醒时间的四分之一
//            newtimequarter = newtime.AddHours(-((double)_materieldata.InventoryDateTime) / 4);
//            // 在生产时间加上提醒时间和生产时间加上提醒时间减去提醒时间四分之一之间，提前提醒用户哪些库存快要到提醒时间
//            if (nowtime < newtime && nowtime >= newtimequarter)
//            {
//                MaterielStorageTimeWarringDto materieldata = new MaterielStorageTimeWarringDto()
//                {
//                    MaterielNum = _invdata[i].MaterielNum,
//                    MaterielName = _invdata[i].MaterielName,
//                    WorkBin = _invdata[i].WorkBinNum,
//                    Icount = (int)_invdata[i].ICountAll,
//                    StorageNum = _invdata[i].StorageNum,
//                    InventoryTime = oldtime,
//                    EarlyWarningTime = newtime,
//                };
//                datas.Add(materieldata);
//            }
//        }
//        return datas;
//    }
//}
